package com.extension.company.common.camera;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.util.Log;

import com.extension.company.common.utils.AppUtils;
import com.extension.company.Constant;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 作者：zwk
 * 时间：on 2019/3/2 16:27
 * 功能：打开摄像头拍照工具类
 */
public class openCameraUtils {

	private final static String TAG = openCameraUtils.class.getSimpleName();

//	public static final String authority = "com.extension.company.admin.fileprovider";


	//	private static Activity mContext;
	private String imgName = "/imge.jpg";
	private static Uri imgUri;


	/**
	 * 使用系统摄像头拍照
	 *
	 * @param context 调用者的上下文
	 * @return 文件的路径
	 */
	public static Uri startActivityForCamera(Activity context) {
//		mContext = context;

		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		File imageFile = createImageFile(context);
		if (imageFile != null) {
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
				imgUri = FileProvider.getUriForFile(context, getAuthority(context), imageFile);  //authority
				Log.d("zwk", "----------" + getAuthority(context));
			} else {
				imgUri = Uri.fromFile(imageFile);
			}
		} else {


		}
		intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
		context.startActivityForResult(intent, Constant.PHOTO_REQUEST_CAMERA);
		return imgUri;
	}

	/**
	 * 拍照后存放的图片的路径
	 *
	 * @param context 上下文
	 * @return 创建的文件夹
	 */
	private static File createImageFile(Context context) {
		String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
		String imageFileName = "JPEG_" + timeStamp + "_";
		File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
		File imageFile = null;
		try {
			imageFile = File.createTempFile(imageFileName, ".jpg", storageDir);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return imageFile;
	}


	/**
	 * 获取相册中图片的真实地址
	 *
	 * @param context 调用方的上下文
	 * @param uri     相册中选中图片的uri
	 * @return 图片的真实路径
	 */
	public static String getImagePath(Activity context, Uri uri) {
		if (null == uri) {
			Log.e("getImagePath", "uri return null");
			return null;
		}

		Log.e("getImagePath", uri.toString());
		String path = null;
		final String scheme = uri.getScheme();
		if (null == scheme) {
			path = uri.getPath();
		} else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
			path = uri.getPath();
		} else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
			String[] proj = {MediaStore.Images.Media.DATA};
			Cursor cursor = context.getContentResolver().query(uri, proj, null, null,
					null);
			int nPhotoColumn = cursor
					.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
			if (null != cursor) {
				cursor.moveToFirst();
				path = cursor.getString(nPhotoColumn);
			}
			cursor.close();
		}

		return path;
	}


	/**
	 * 获取文件的读写权限的地址
	 *
	 * @param context 调用方的上下文
	 * @return authority 权限地址
	 */
	private static String getAuthority(Context context) {
		return AppUtils.getPackageName(context) + ".fileprovider";
	}
}
